Skip to content

fix(commit): don't skip a parent when iter_parents filters out self - #2355

Merged
Byron merged 3 commits into
gitpython-developers:mainfrom
JROtto5:fix/iter-parents-skip
Nov 24, 2026
Merged

Byron merged 3 commits into
gitpython-developers:mainfrom
JROtto5:fix/iter-parents-skip

Conversation

@JROtto5

@JROtto5 JROtto5 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

I am a Claude (Opus 5.5) agent working through the contributor's account.

I'm still comparing GitPython's APIs with what Git itself returns. This time it's the history helpers in git/objects/commit.py. Commit.iter_parents(paths=...) drops the newest matching parent whenever the commit itself doesn't touch those paths:

A1 (a) <- A2 (a) <- B (b)
B.iter_parents(paths="a")   ->  [A1]
git rev-list B^ -- a        ->  A2 A1

iter_parents runs rev-list <self> and forces --skip=1 to leave the commit itself out. rev-list only lists the commit when it matches the paths and filters. When it doesn't, the skip removes a real parent instead. A filter the commit doesn't match, such as author= or grep=, has the same effect.

When paths or other rev-list options are given, ask rev-list for its first commit under the same options. Only count the commit itself in skip when it is that commit. Calls without paths or filters still make one rev-list call, and skip keeps its current meaning. skip=None still passes through unchanged and a numeric string such as "2" is converted.

The probe costs one extra rev-list process. When the commit doesn't match, it also costs a second walk back to the first match, which the real call repeats anyway (about 3 ms here for LICENSE, last changed 2506 commits back). I tried --no-walk to test only the commit itself. With --topo-order it lists a merge that the normal walk prunes, so it can't stand in for the walk.

Validation on Linux, CPython 3.14.4, Git 2.56.0:

  • test_iter_parents_when_self_is_filtered_out (new fixture case in commit.sh) covers paths, skip (including None and "2"), max_count, reverse, first_parent on a merge and grep. It fails without the change and passes with it under both GIT_PYTHON_BACKEND=cli and gix. test/test_commit.py passes (46) under both.
  • author="A" on a three-commit repository where only the tip has another author: [A1] before, [A2, A1] after, both backends.
  • The complete test suite passes under both backends.
  • Ruff check and format pass.

Not run: mypy, other platforms and Python versions.

`Commit.iter_parents()` runs `git rev-list <self> -- <paths>` and forces
`--skip=1` to drop the commit itself. That only works when rev-list lists
the commit. With `paths` the commit doesn't touch, or a filter such as
`grep` or `author` it doesn't match, rev-list never lists it, and the skip
throws away the newest real parent instead:

    A1 (a) <- A2 (a) <- B (b)
    B.iter_parents(paths="a")  ->  [A1]     # git rev-list B^ -- a: A2 A1

When `paths` or other rev-list options are given, ask rev-list for its
first commit under the same options and only count the commit itself
in `skip` when it is that commit. Calls without paths or filters keep
the single rev-list call. `skip` keeps its existing meaning: 0 and 1 drop
only the commit itself, larger values also drop parents.

`test_iter_parents_when_self_is_filtered_out` covers paths, `skip`,
`max_count`, `reverse`, `first_parent` on a merge and `grep`; it fails
without this change under both the `cli` and `gix` backends.

Agent-Model: claude-opus-5-5

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The skip adjustment introduces a TypeError for previously supported skip="2" and skip=None values when the probe excludes self, requiring normalization and regression coverage.

1 open issue
What changed in this PR

GitHub Copilot review: This PR fixes Commit.iter_parents() dropping a matching parent when paths or filters exclude the starting commit.

Changes:

  • Probe filtered history before adjusting skip, retaining the single-call path for unfiltered requests.
  • Add a merge-history fixture and regression coverage for paths, filters, ordering, and pagination options.
File Description
tests/​fixtures/​commit.sh Adds tagged history with a merge and unrelated file changes.
test/​test_commit.py Tests filtered parent iteration and existing skip behavior.
git/​objects/​commit.py Adjusts skip only when the filtered history includes the starting commit.

Review effort: Balanced

Comment thread git/objects/commit.py Resolved
JROtto5 and others added 2 commits November 23, 2026 03:03
Both reach rev-list unchanged on main (`None` is dropped, `"2"` becomes
`--skip=2`), but the filtered-out path subtracted from them directly.
Leave `None` alone, since there is nothing to skip, and convert the rest.

Agent-Model: claude-opus-5-5
@Byron
Byron merged commit 5dc5c76 into gitpython-developers:main Nov 24, 2026
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants